
// 微信端开放的接口
const publicArr = [/^\/wechat\//,]
function rolejudge(req,res,next) {
    // req.token = req.headers.authorization;
  // console.log(req.token);
  // 判断token解析的数据中role的值
  // console.log(req.auth);
  if (!req.auth) {
    return next()
  }

  let flag = false;
  // 验证通过
  for (let i in publicArr) {
    const item = publicArr[i]
    flag = item.test(req.path)
    if (flag) break;
  }
  // console.log("flag=",flag);
  const _id = req.auth['_id']
  // console.log("_id",!_id);
  if (flag) {
    // 微信小程序段的token验证
    if (!_id || _id.length==0) {
      return res.onError("登录已过期，请重新登录！")
    }
    return next();
  }else {
    if (_id&&_id.length>=0) {
      return res.onError("登录已过期，请重新登录！")
    }
  }
  const role = req.auth.role;
  // console.log(role);
  if (role === 0) {
    if (isEqual(req.method, "GET")) {
      return next()
    } else {
      return res.onError("权限不足，无法操作")
    }
  }
  // console.log("结束");
  return next()
  // 判断请求类型 
  // if(role===0&&req.method)
}
module.exports = {
    rolejudge
}